﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BenefitHelper.View
{
    /// <summary>
    /// 交易员日按受益情况分
    /// </summary>
    public class OperatorDayWinNum
    {
        public List<OperatorDayWinNumData> Datas { get; set; }

        public OperatorDayWinNum GetOperatorDayWinNum(int tradeHistoryId, int operatorid, bool win, DB.DBManager db)
        {
            OperatorDayWinNum info = new OperatorDayWinNum();
            info.Datas = new List<OperatorDayWinNumData>();
            List<Sys.Account> accounts = new Sys.DayOperatorAccount().GetDayOperatorAccounts(new Sys.TradeHistory().GetDateTimeFromTradeHistoryId(tradeHistoryId, db), operatorid, db);
            foreach (Sys.Account account in accounts)
            {
                List<Data.Day.AccountDayTradeProduct> infos = new Data.Day.AccountDayTradeProduct().GetAccountDayTradeProduct(account.Id, tradeHistoryId, win, db);
                foreach (Data.Day.AccountDayTradeProduct i in infos)
                {
                    double abs = Math.Abs(i.Profit);
                    int level = 0;
                    if (abs >= 0 && abs <= 300)
                    {
                        level = 300;
                    }
                    else if (abs > 300 && abs <= 600)
                    {
                        level = 600;
                    }
                    else if (abs > 600 && abs <= 900)
                    {
                        level = 900;
                    }
                    else if (abs > 900 && abs <= 1200)
                    {
                        level = 1200;
                    }
                    else if (abs > 1200 && abs <= 1500)
                    {
                        level = 1500;
                    }
                    else if (abs > 1500 && abs <= 1800)
                    {
                        level = 1800;
                    }
                    else if (abs > 1800 && abs <= 2100)
                    {
                        level = 2100;
                    }
                    else
                    {
                        level = 2400;
                    }
                    var query = info.Datas.Where(a => a.Level == level).ToList();

                    if (query.Count() > 0)
                    {
                        OperatorDayWinNumData data = query.First();
                        data.BillCount = data.BillCount + i.BillCount;
                        data.SumValue = data.SumValue + Math.Abs(i.Profit);
                    }
                    else
                    {
                        OperatorDayWinNumData data = new OperatorDayWinNumData();
                        data.Level = level;
                        data.BillCount = i.BillCount;
                        data.SumValue = Math.Abs(i.Profit);
                        info.Datas.Add(data);
                    }
                }
            }
            var allSum = info.Datas.Sum(a => a.SumValue);
            foreach (OperatorDayWinNumData a in info.Datas)
            {
                a.Percent = Convert.ToInt32((a.SumValue / allSum) * 100);
            }
            return info;
        }
    }
    public class OperatorDayWinNumData
    {
        public int Id { get; set; }

        public int OperatorId { get; set; }

        public virtual Sys.Operator Operator { get; set; }

        public int TradeHistoryId { get; set; }

        public virtual Sys.TradeHistory TradeHistory { get; set; }

        public int Level { get; set; }

        public double BillCount { get; set; }

        public double SumValue { get; set; }

        public double Percent { get; set; }
    }
}
